﻿/*
培训任务的添加修改
*/
//
angular.module('editquestionnaire', ['vModal', 'loadModule', 'PensonSelector','Common'])
// 设置焦点 标签用
.directive('setFocus', function () {
     return function (scope, element) {
         element[0].focus();
     };
 })
 .directive("editquestionnaire", ["$rootScope", "sysUserInfo", "$timeout", "Msg","interlocutionservice",function ($rootScope, sysUserInfo, $timeout, Msg,IS) {
     return {
         restrice: "AE",
         scope: {
             itemObj: "=",   // 问卷对象
             callback:"&",//回调
             onClose: "&"   //关闭窗口       
         },
         templateUrl: "/system/directive/honor/edit_questionnaire.html",
         link: function (scope, element, attr) {

           scope.type=1;
           //领导打分
           if(scope.itemObj.questionnaireScene!=undefined&&scope.itemObj.questionnaireScene!=null&&scope.itemObj.questionnaireScene==3){
                scope.type=3;
           }

           //切换问卷使用模式
           scope.checkedType=function(index){
                scope.type=index;
           }
           scope.isSaveBtn=false;
             //新增的参数
            scope.addParams = {
                orgid: sysUserInfo.getuserinfo().organization_ID,
                isPublic: false,  // 按照培训任务名称搜索
                userJson:"",//用户json，前端用来回显  
                userid: sysUserInfo.getuserinfo().user_ID,
                questionnaireId : scope.itemObj.questionnaireId,//问卷id
                department: "",  // 用户组织机构id
                role: "", // 角色 id
                userinfo:"",//用户id
                usergroup: "", //用户组id
                startTime:"",//开始日期
                endTime:""//结束日期
            }
            scope.mousePosition = function (ev) {
                if (!ev) {
                    ev = window.event;
                }

                if (ev.type != "click") {
                    return;
                }
                var width = 0;
                var height = 0;
                if (ev.pageX || ev.pageY) {//firefox、chrome等浏览器
                    if (document.documentElement.clientWidth - ev.pageX < 450) {
                        width = ev.pageX - (450 - (document.documentElement.clientWidth - ev.pageX)) - 20;
                    } else {
                        width = ev.pageX;
                    }
                    if (document.documentElement.clientHeight - ev.pageY < 480) {
                        height = ev.pageY - (480 - (document.documentElement.clientHeight - ev.pageY)) - 20;
                    } else {
                        height = ev.pageY;
                    }

                    return { "left": width + "px", "top": height + "px", "position": "fixed" };
                }

                return {// IE浏览器
                    "left": ev.clientX + document.body.scrollLeft - document.body.clientLeft,
                    "top": ev.clientY + document.body.scrollTop - document.body.clientTop
                };
            }            
            // 初始化
            scope.init = function(){                               
                // 人员选择器                
                 scope.stuData = {
                     menu:[{ //进入人员选择器的菜单
                        "id":"User"
                        },{ "id":"Org"},
                        {"id":"Group"},
                        {"id":"Role"}                
                     ],
                     ApiOpen: false,
                     windowModal:1,
                     style: "",                                      
                     ViceuserOrgList: [],
                     ViceuserRoleList: [],
                     ViceuserGroupList: [],
                     searchType: [],  //初始化教师查询
                     //searchTexts: ,  // 初始化教师查询
                     DB: "0",
                     DBdata: [],
                     UserList: [],
                     userOrgList:[],                 
                     userGroupList:[],
                     userRoleList:[]
                 }
                 // 扩展字段
                 if(!scope.itemObj.hasOwnProperty('userJson')){
                        scope.itemObj.userJson = {};
                 }else{ // 后台返回
                         if(typeof(scope.itemObj.userJson)=="string"){
							 if(scope.itemObj.userJson.length>0&&scope.itemObj.userJson!="\"\""){
									scope.itemObj.userJson = JSON.parse(scope.itemObj.userJson);
							 }else{
								scope.itemObj.userJson={};
							 }
						}
                 }
                
                if(scope.itemObj.userJson.UserList==undefined){
                    scope.itemObj.userJson.UserList=[];
                }
                if(scope.itemObj.userJson.userOrgList==undefined){
                    scope.itemObj.userJson.userOrgList=[];
                }
                if(scope.itemObj.userJson.userRoleList==undefined){
                    scope.itemObj.userJson.userRoleList=[];
                }
                if(scope.itemObj.userJson.userGroupList==undefined){
                    scope.itemObj.userJson.userGroupList=[];
                }
                 // 向人员选择器中赋值
                 scope.stuData.UserList = scope.itemObj.userJson.UserList; // 人员
                 scope.stuData.userOrgList = scope.itemObj.userJson.userOrgList;  // 部门
                 scope.stuData.userRoleList = scope.itemObj.userJson.userRoleList; // 角色
                 scope.stuData.userGroupList = scope.itemObj.userJson.userGroupList;   // 组织机构          
                 
                 // 参与用户群的样式                
                if(scope.itemObj.isPublic){
                    scope.stuParticipateStyle = { "background":"#eee"}; 
                }else{
                    scope.stuParticipateStyle={}; 
                }
                scope.addParams.isPublic=scope.itemObj.isPublic;
                scope.addParams.startTime=(scope.itemObj.startTime==undefined?"":scope.itemObj.startTime);
                scope.addParams.endTime=(scope.itemObj.endTime==undefined?"":scope.itemObj.endTime);
                scope.addParams.dateFormate = (scope.itemObj.dateFormate==undefined?"":scope.itemObj.dateFormate);
            }

            scope.init();
             //样式
            // 文本框的样式
            scope.taskNameStyle = {                
                "background-color": "#e1e1e1",
            };
            //获取焦点时样式
            scope.taskNameFocusStyle = function(){
                 scope.taskNameStyle = {                    
                    "background-color": "#e1e1e1",                    
                };
            };
            // 失去焦点时样式
            scope.taskNameBlurStyle = function(){
                scope.taskNameStyle = {                    
                    "background-color": "#eee",                    
                };
            };
            
            // 是否全部学员参与
            scope.checkedStuParticipate = function(){               
                if(scope.addParams.isPublic){    //选中状态                    
                    scope.addParams.isPublic = false;                
                    scope.stuParticipateStyle={};
                }else{
                    // 初始化参与群
                    scope.stuData.UserList=[]; // 人员
                    scope.stuData.userOrgList= [];  // 部门
                    scope.stuData.userRoleList = []; // 角色
                    scope.stuData.userGroupList =[];   // 组织机构
                    scope.stuParticipateStyle = { "background":"#eee"};
                    scope.addParams.isPublic = true;       

                }
            }
     
            /*****************************人员选择器开始**************************************/
            
             // 选择用户群
             scope.selTaskStu = function(event){
                    if(scope.itemObj.is_public){ // 是否是全员参与
                        Msg.info("目前已经是全员参与了！");
                    }else{
                        scope.stuData.style=scope.mousePosition(event);
                        scope.stuData.ApiOpen = true;            
                   }                       
             }
             // 删除用户群
             scope.delStu = function(item,type){
                switch (type) {
                    case "1":   // 用户
                         angular.forEach(scope.stuData.UserList,function(data,index,array){
                            if(data.user_ID == item.user_ID){
                                scope.stuData.UserList.splice(index,1); //删除当前的
                                return;                           
                            }                                 
                         });                     
                    break;  
                    case "2":    // 部门                    
                         angular.forEach(scope.stuData.userOrgList,function(data,index,array){
                            if(data.organization_ID == item.organization_ID){
                                scope.stuData.userOrgList.splice(index,1); //删除当前的
                                return;                           
                            }                                 
                        });
                    break;
                    case "3":   // 角色                     
                      angular.forEach(scope.stuData.userRoleList,function(data,index,array){
                            if(data.roles_ID == item.roles_ID){
                                scope.stuData.userRoleList.splice(index,1); //删除当前的
                                return;                           
                            }                                 
                        });
                    break;
                    case "4":   // 用户组                    
                        angular.forEach(scope.stuData.userGroupList,function(data,index,array){
                            if(data.userGroup_ID == item.userGroup_ID){
                                scope.stuData.userGroupList.splice(index,1); //删除当前的
                                return;                           
                            }                                 
                        });
                    break;
                }
             }
            /*****************************人员选择器结束**************************************/            
           
            // 设置培训类型   0 不限制 1周期 2 天
            scope.setDateType=function(ind){
                scope.addParams.dateFormate = ind;
            }
            //日期选择
            scope.GetDate = function () {
                scope.addParams.startTime= document.getElementById("startTime").value;
                scope.addParams.endTime= document.getElementById("endTime").value;
            }
            // 保存
            scope.btnSave = function(){
                    scope.itemObj.userJson.UserList  = scope.stuData.UserList; // 人员
                    scope.itemObj.userJson.userOrgList = scope.stuData.userOrgList;  // 部门
                    scope.itemObj.userJson.userRoleList = scope.stuData.userRoleList; // 角色
                    scope.itemObj.userJson.userGroupList = scope.stuData.userGroupList;   // 组织机构         

                    //用户
                    angular.forEach(scope.stuData.UserList,function(data,index,array){                            
                        scope.itemObj.name += data.user_Name +',';             
                        scope.addParams.userinfo += (scope.addParams.userinfo.length <= 0 ? "" : ",") + data.user_ID;                           
                    });    
                    //部门
                    angular.forEach(scope.stuData.userOrgList,function(data,index,array){                           
                        scope.itemObj.name += data.organization_Name +',';                  
                        scope.addParams.department += (scope.addParams.department.length <= 0 ? "" : ",") + data.organization_ID;          
                    });    
                    // 角色
                    angular.forEach(scope.stuData.userRoleList,function(data,index,array){
                        scope.itemObj.name += data.roles_Name +',';
                        scope.addParams.role += (scope.addParams.role.length <= 0 ? "" : ",") + data.roles_ID;
                    });    
                    // 用户组
                    angular.forEach(scope.stuData.userGroupList,function(data,index,array){                            
                        scope.itemObj.name += data.userGroup_Name +',';
                         scope.addParams.usergroup += (scope.addParams.usergroup.length <= 0 ? "" : ",") + data.userGroup_ID;
                    });                

                //判断参与人员
                if (!scope.addParams.isPublic&&!scope.isUserNull()){
                    Msg.warning("请选择参与人群！");
                    return;
                }
               scope.isSaveBtn=true;

                scope.addParams.userJson=JSON.stringify(scope.itemObj.userJson);
                if(scope.addParams.isPublic==undefined||scope.addParams.isPublic.length<=0){
                    scope.addParams.isPublic=false;
                }
                if(scope.type==3||scope.type=="3"){
                    scope.addParams.type=scope.type;
                }else{
	                scope.addParams.type="0";
	            }
                // 发送请求
                IS.distributeQuestionnaire(scope.addParams).then(function (respose) {
                    scope.isSaveBtn = false;
                    if (respose.errorcode == "0") {
                        Msg.success("分配成功！");
                        scope.callback();
                        scope.onClose();  // 关闭窗口
                    }else if(respose.errorcode == "4"){
                        Msg.warning("账号不存在！");
                    }else if(respose.errorcode == "26") {
                        Msg.warning("无权限操作！");
                    } else if(respose.errorcode == "25") {
                        Msg.warning("问卷不存在！");
                    }else {
                        Msg.error("操作失败！");
                    }
                }, function errorCallback() {
                    console.log("连接服务器失败！");
                    scope.isSaveBtn = false;
                });

            }
            // 判断参与人群
            scope.isUserNull = function(){
                var a=false;
                if(scope.itemObj.userJson.hasOwnProperty('UserList')&&scope.itemObj.userJson.UserList.length>0){ // 用户
                    a= true;      
                }  
                if(scope.itemObj.userJson.hasOwnProperty('userOrgList')&&scope.itemObj.userJson.userOrgList.length>0){ // 部门
                    a= true;                         
                } 
                if(scope.itemObj.userJson.hasOwnProperty('userRoleList')&&scope.itemObj.userJson.userRoleList.length>0){ //角色 
                    a=true;   
                }
                if(scope.itemObj.userJson.hasOwnProperty('userGroupList')&&scope.itemObj.userJson.userGroupList.length>0){ //用户组
                    a=true;            
                }           
                return a;     
            }

         }
         
     }
 } ]);
